// components/login/login.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    authData: {
      type: Object,
      value: {
        authType: "0", //0表示使用登录弹窗  1表示使用完善昵称头像弹窗
        authUsername: "false", //是否已经授权头像昵称
      },
    },
    showLogin: {
      type: Boolean,
      value: true
    },
    openid: {
      type: String,
      value: ''
    },
    showWsInfoDom: {
      type: Boolean,
      value: true
    },
    showPrivacyFlag: {
      type: Boolean,
      value: true
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    avatarUrl: app.baseUrl + "images/login/default.png",
    nickName: null,
    showWsInfoDom: false, //是否显示头像昵称填写弹窗
    showLogin: false,
    showPrivacyFlag: app.globalData.showPrivacyFlag, //是否点击了同意了隐私指引
    is_nickAndHead: 0,
    nickname: '',
    head: '',
    items: [{
        value: 0,
        name: "同意"
      },
      {
        value: 1,
        name: "拒绝"
      }
    ],
  },
  lifetimes: {
    ready() {

      wx.getPrivacySetting({
        
        success: res => {
          console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
          if (res.needAuthorization) {
            // 需要弹出隐私协议
            this.setData({
              showPrivacyFlag: false
            });
            app.globalData.showPrivacyFlag = false;
          } else {
            this.setData({
              showPrivacyFlag: true
            });
            app.globalData.showPrivacyFlag = true;
          }
        },
        fail: () => {},
        complete: () => {}
      });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
  
    /**
     * 选择头像返回信息监听
     */
    chooseavatar(res) {
      const avatarUrl = res.detail.avatarUrl
      this.setData({
        avatarUrl: avatarUrl
      })
    },
    /** 获取昵称信息 */
    bindblur(res) {
      const value = res.detail.value
      this.data.nickName = value
    },

    /**
     * 设置信息按钮点击监听
     */
    setBtnTap() {
      var that = this;
      const {
        avatarUrl,
        nickName
      } = this.data
      console.log(avatarUrl.indexOf("default"))
      if (avatarUrl.indexOf("default") > 0) {
        app.showTip("请上传头像");
        return;
      }
      if (nickName == null) {
        app.showTip("请输入您的昵称");
        return;
      }

      wx.showLoading({
        title: '数据提交中...'
      });
     
      wx.uploadFile({
        url: that.data.baseUrl + "addWxUserInfo.php", //服务器接口
        method: 'POST',
        filePath: avatarUrl,
        header: {
          'content-type': 'multipart/form-data'
        },
        name: 'file', //服务器定义的Key值
        formData: {
          "openid": that.data.openid,
          'nickname': nickName
        },
        success: function (res) {
          wx.hideLoading();
          //添加成功
          if (res.data && res.data != -1 && res.data != -2 && res.data != 0) {

            that.setData({
              showWsInfoDom: false,
              nickname: nickName,
              is_nickAndHead: 1,
              head: res.data
            });

            //调用回传
            that.returnVal();
          } else if (res.data == -1) {
            app.showTip('昵称有敏感词汇,请更新昵称');
          } else if (res.data == -2) {
            app.showTip('头像用图存在敏感内容,请更换头像');
          } else {
            app.showTip('信息保存失败,请刷新页面重试');
          }
        },
        fail: function () {
          wx.hideLoading();
          app.showTip('服务器繁忙,请稍候重试');
        }
      });

    },
    wsInfo() {
      this.setData({
        showWsInfoDom: true,
        showLogin: false
      })
    },
    closeWsInfo() {
      this.setData({
        showWsInfoDom: false
      })
    },
    close() {
      this.setData({
        showLogin: false
      })
    },

    //电话及头像昵称授权后向父组件传值
    returnVal() {
      var that = this;
      this.triggerEvent('returnVal', {
        is_nickAndHead: that.data.is_nickAndHead,
        nickname: that.data.nickname,
        head: that.data.head
      })
    },

    //关闭指引
    closeZy() {
      this.setData({
        showLogin: false,
        showWsInfoDom: false
      });
    },

    //点击查看协议指引
    handleOpenPrivacyContract() {
      // 打开隐私协议页面
      wx.openPrivacyContract({
        success: () => {}, // 打开成功
        fail: () => {}, // 打开失败
        complete: () => {}
      })
    },

    //点击同意协议
    handleAgreePrivacyAuthorization() {
      this.setData({
        showPrivacyFlag: true
      });
      app.globalData.showPrivacyFlag = true;
    },

    //处理父级页面打开子组件时传递给子组件的值
    openLoginWin() {
      this.setData({
        openid: this.properties.openid,
        showWsInfoDom: this.properties.showWsInfoDom,
        showLogin: this.properties.showLogin,
        showPrivacyFlag: this.properties.showPrivacyFlag
      })
    }

  }
})